Make (software)

make
Original author(s) Stuart Feldman
Initial release 1977
Type build automation tool

In software development, Make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. Make can decide where to start through topological sorting. Though integrated development environments and language-specific compiler features can also be used to manage the build process in modern systems, Make remains widely used, especially in Unix.

Contents

Origin

There are now a number of dependency-tracking build utilities, but Make is one of the most widespread, primarily due to its inclusion in Unix, starting with the PWB/UNIX 1.0, which featured a variety of tools targeting software development tasks. It was originally created by Stuart Feldman in 1977 at Bell Labs. In 2003 Dr. Feldman received the ACM Software System Award for the authoring of this widespread tool.[1]

Before Make's introduction, the Unix build system most commonly consisted of operating system dependent "make" and "install" shell scripts accompanying their program's source. Being able to combine the commands for the different targets into a single file and being able to abstract out dependency tracking and archive handling was an important step in the direction of modern build environments.

Modern versions

Make has gone through a number of rewrites, including a number of from-scratch variants which used the same file format and basic algorithmic principles and also provided a number of their own non-standard enhancements. Some of them are:

POSIX includes standardization of the basic features and operation of the Make utility, and is implemented with varying degrees of completeness in Unix-based versions of Make. In general, simple makefiles may be used between various versions of Make with reasonable success. GNU Make and BSD Make can be configured to look first for files named "GNUmakefile" and "BSDmakefile" respectively,[2][3] which allows one to put makefiles which use implementation-defined behavior in separate locations.

Behaviour

Make is typically used to build executable programs and libraries from source code. Generally though, any process that involves transforming a source file to a target result (by executing arbitrary commands) is applicable to Make. For example, Make could be used to detect a change made to an image file (the source) and the transformation actions might be to convert the file to some specific format, copy the result into a content management system, and then send e-mail to a predefined set of users that the above actions were performed.

Make is invoked with a list of target file names to build as command-line arguments:

    make TARGET [TARGET ...]

Without arguments, Make builds the first target that appears in its makefile, which is traditionally a symbolic "phony" target named all.

Make decides whether a target needs to be regenerated by comparing file modification times. This solves the problem of avoiding the building of files which are already up to date, but it fails when a file changes but its modification time stays in the past. Such changes could be caused by restoring an older version of a source file, or when a network filesystem is a source of files and its clock or timezone is not synchronized with the machine running Make. The user must handle this situation by forcing a complete build. Conversely, if a source file's modification time is in the future, it triggers unnecessary rebuilding, which may inconvenience users.

Makefiles

Make searches the file to run from current directory. E.g. the GNU Make searches files in order GNUmakefile, makefile, Makefile.

The makefile language is similar to declarative programming.[4][5][6][7] This class of language, in which necessary end conditions are described but the order in which actions are to be taken is not important, is sometimes confusing to programmers used to imperative programming.

One problem in build automation is the tailoring of a build process to a given platform. For instance, the compiler used on one platform might not accept the same options as the one used on another. This is not well handled by Make. This problem is typically handled by generating platform specific build instructions, which in turn are processed by Make. Common tools for this process are Autoconf and CMake.

Rules

A makefile consists of rules. Each rule begins with a textual dependency line which defines a target followed by a colon (:) and optionally an enumeration of components (files or other targets) on which the target depends. The dependency line is arranged so that the target (left hand of the colon) depends on components (right hand of the colon).

An example is a specific object file target which depends on a C source file and header files. Because Make itself does not understand, recognize or distinguish different kinds of files, this opens up a possibility for human error. A forgotten or an extra dependency may not be immediately obvious and may result in subtle bugs in the generated software. It is possible to write makefiles which generate these dependencies by calling third-party tools, and some makefile generators, such as the Automake toolchain provided by the GNU Project, can do so automatically.

After each dependency line, a series of command lines may follow which define how to transform the components (usually source files) into the target (usually the "output"). If any of the components have been modified, the command lines are run.

Each command line must begin with a tab character to be recognized as a command. The tab is a whitespace character, but the space character does not have the same special meaning. This is problematic, since there may be no visual difference between a tab and a series of space characters. This aspect of the syntax of makefiles is often subject to criticism. When using makefile generators or text editors with explicit makefile support, this issue is likely less important.

Each command is executed by a separate shell or command-line interpreter instance. Since operating systems use different command-line interpreters this can lead to unportable makefiles. For instance, GNU Make by default executes commands with /bin/sh, where Unix commands like cp are normally used. In contrast to that Microsoft's nmake executes commands with cmd.exe, where batch commands like copy will be used.

    target [target ...]: [component ...]
    [<TAB>command 1]
	   .
	   .
	   .
    [<TAB>command n]

Usually each rule has a single unique target, rather than multiple targets.

A rule may have no command lines defined. The dependency line can consist solely of components that refer targets like:

    realclean: clean distclean

The command lines of a rule are usually arranged so that they generate the target. An example: if "file.html" is newer, it is converted to text. The contents of the makefile:

    file.txt: file.html
	    lynx -dump file.html > file.txt

The above rule would be triggered when Make updates "file.txt". In the following invocation, Make would typically use this rule to update the "file.txt" target if necessary.

    make file.txt

Command lines can have one or more of the following three prefixes:

Ignoring errors and silencing echo can alternatively be obtained via the special targets ".IGNORE" and ".SILENT".[8]

Macros

A makefile can contain definitions of macros. Macros are usually referred to as variables when they hold simple string definitions, like "CC=gcc". Macros in makefiles may be overridden in the command-line arguments passed to the Make utility. Environment variables are also available as macros.

Macros allow users to specify the programs invoked and other custom behavior during the build process. For example, the macro "CC" is frequently used in makefiles to refer to the location of a C compiler, and the user may wish to specify a particular compiler to use.

New macros (or simple "variables") are traditionally defined using capital letters:

    MACRO = definition

A macro is used by expanding it. Traditionally this is done by enclosing its name inside $(). Very rarely, though possible, inside ${}.

    NEW_MACRO = $(MACRO)-$(MACRO2)

Macros can be composed of shell commands by using the command substitution operator, denoted by backticks (`).

    YYYYMMDD  = ` date `

The content of the definition is stored "as is". Lazy evaluation is used, meaning that macros are normally expanded only when their expansions are actually required, such as when used in the command lines of a rule. An extended example:

    PACKAGE   = package
    VERSION   = ` date +"%Y.%m%d" `
    ARCHIVE   = $(PACKAGE)-$(VERSION)
 
    dist:
            #  Notice that only now macros are expanded for shell to interpret:
            #      tar -cf package-`date +"%Y%m%d"`.tar
 
            tar -zcf $(ARCHIVE).tar .

The generic syntax for overriding macros on the command line is:

    make MACRO="value" [MACRO="value" ...] TARGET [TARGET ...]

Makefiles can access predefined internal macros.

    target: component1 component2
            echo $? contains those components, that need attention, THAT ARE YOUNGER than current TARGET
            echo $@ evaluates to current TARGET name to the left

Suffix rules

Suffix rules are in the form FROM.TO and they can be used to launch actions based on file extension. In the case of suffix rules, internal macro $< refers the component and $@ refers to the TARGET. An example to convert any HTML file into text; notice the shell redirection token > in the middle:

    .SUFFIXES: .txt .html
 
    # From .html to .txt
    .html.txt:
            lynx -dump $<   >   $@

When called from command line, the above example expands.

    $ make -n file.txt
      lynx -dump file.html > file.txt

Other elements

Single-line comments are started with the hash symbol (#).

Some directives in makefiles can include other makefiles.

Line continuation is indicated with a backslash \ character at the end of a line.

    target: component \
            component
    <TAB>command ;          \
    <TAB>command |          \
    <TAB>piped-command

Example makefiles

Makefiles are traditionally used for compiling code (*.c, *.cc, *.C, etc.), but they can also be used for providing commands to automate common tasks. One such makefile is called from the command line:

    make                        # Without argument runs first TARGET
    make help                   # Show available TARGETS
    make dist                   # Make a release archive from current dir

The makefile:

    PACKAGE	 = package
    VERSION	 = ` date "+%Y.%m%d%" `
    RELEASE_DIR  = ..
    RELEASE_FILE = $(PACKAGE)-$(VERSION)
 
    # Notice that the variable LOGNAME comes from the environment in
    # POSIX shells.
    #
    # target: all - Default target. Does nothing.
    all:
	    echo "Hello $(LOGNAME), nothing to do by default"
            # very rarely: echo "Hello ${LOGNAME}, nothing to do by default"
	    echo "Try 'make help'"
 
    # target: help - Display callable targets.
    help:
	    egrep "^# target:" [Mm]akefile
 
    # target: list - List source files
    list:
	    # Won't work. Each command is in separate shell
	    cd src
	    ls
 
	    # Correct, continuation of the same shell
	    cd src; \
	    ls
 
    # target: dist - Make a release.
    dist:
	    tar -cf  $(RELEASE_DIR)/$(RELEASE_FILE) && \
	    gzip -9  $(RELEASE_DIR)/$(RELEASE_FILE).tar

Below is a very simple makefile that would compile a source called "helloworld.c" using gcc, a C compiler, and specifies a "clean" target to remove the generated files, for example to start over. The $@ and $< are two of the so-called internal macros (also known as automatic variables) and stand for the target name and "implicit" source, respectively. In the example below, $^ expands to a space delimited list of the prerequisites. There are a number of other internal macros.[9][10]

    CC     = gcc
    CFLAGS = -g
 
    all: helloworld
 
    helloworld: helloworld.o
	    # Commands start with TAB not spaces
	    $(CC) $(LDFLAGS) -o $@ $^
 
    helloworld.o: helloworld.c
	    $(CC) $(CFLAGS) -c -o $@ $<
 
    clean: FRC
	    rm -f helloworld helloworld.o
 
    # This pseudo target causes all targets that depend on FRC
    # to be remade even in case a file with the name of the target exists.
    # This works with any make implementation under the assumption that
    # there is no file FRC in the current directory.
    FRC:

Many systems come with a version of Make configured to handle common tasks like compiling based on file suffixes, which allows one to leave out the actual instructions from the target and source specification. On such a system the above makefile could be modified as follows:

    all: helloworld
 
    helloworld: helloworld.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^	 
 
    clean: FRC
	rm -f helloworld helloworld.o
 
    # This is an explicit suffix rule. It may be omitted on systems
    # that handle simple rules like this automatically.
    .c.o:
	$(CC) $(CFLAGS) -c $<
 
    FRC:
    .SUFFIXES: .c

That "helloworld.o" depends on "helloworld.c" is now automatically handled by Make. In such a simple example as the one illustrated here this hardly matters, but the real power of suffix rules becomes evident when the number of source files in a software project starts to grow. One only has to write a rule for the linking step and declare the object files as prerequisites. Make will then implicitly determine how to make all the object files and look for changes in all the source files.

Simple suffix rules work well as long as the source files do not depend on each other and on other files such as header files. Another route to simplify the build process is to use so-called pattern matching rules that can be combined with compiler-assisted dependency generation. As a final example requiring the gcc compiler and GNU Make, here is a generic makefile that compiles all C files in a folder to the corresponding object files and then links them to the final executable. Before compilation takes place, dependencies are gathered in makefile-friendly format into a hidden file ".depend" that is then included to the makefile.

    # Generic GNUMakefile
 
    # Just a snippet to stop executing under other make(1) commands
    # that won't understand these lines
    ifneq (,)
    This makefile requires GNU Make.
    endif
 
    PROGRAM = foo
    C_FILES := $(wildcard *.c)
    OBJS := $(patsubst %.c, %.o, $(C_FILES))
    CC = cc
    CFLAGS = -Wall -pedantic 
    LDFLAGS =
 
    all: $(PROGRAM)
 
    $(PROGRAM): .depend $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $(PROGRAM)
 
    depend: .depend
 
    .depend: cmd = gcc -MM -MF depend $(var); cat depend >> .depend;
    .depend: 
	@echo "Generating dependencies..."
	@$(foreach var, $(C_FILES), $(cmd))
	@rm -f depend
 
    -include .depend
 
    # These are the pattern matching rules. In addition to the automatic
    # variables used here, the variable $* that matches whatever % stands for
    # can be useful in special cases.
    %.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@
 
    %: %.c
	$(CC) $(CFLAGS) -o $@ $< 
 
    clean:
	rm -f .depend *.o
 
    .PHONY: clean depend

See also

References

  1. ^ Matthew Doar (2005). Practical Development Environments. O'Reilly Media. pp. 94. ISBN 978-0596007966. 
  2. ^ "GNU `make'". Free Software Foundation. http://www.gnu.org/software/make/manual/make.html#Makefile-Names. 
  3. ^ "Manual Pages: make". OpenBSD 4.8. http://www.openbsd.org/cgi-bin/man.cgi?query=make#FILES. 
  4. ^ an overview on dsls, 2007/02/27, phoenix wiki
  5. ^ http://www.cs.ualberta.ca/~paullu/C201/Slides/c201.21-31.pdf
  6. ^ Re: Choreography and REST, from Christopher B Ferris on 2002-08-09
  7. ^ Target Junior Makefiles, Andrew W. Fitzgibbon and William A. Hoffman
  8. ^ make, The Open Group Base Specifications Issue 6
  9. ^ A listing of some Make internal macros at opengroup.org
  10. ^ Automatic Variables GNU `make'

External links

Reference, manuals, books

Tutorials

Debugging GNU Make

Other